<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" label-position="top" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" :disabled="disabled" label-width="50px" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="活动标识" prop="activityCode">
        <el-input v-model="dataForm.activityCode" :disabled="disabled" label-width="50px" placeholder="活动标识"></el-input>
      </el-form-item>
      <el-form-item label="是否展示" prop="isShow">
        <el-dict code="IS_NOT" :disabled="disabled" v-model="dataForm.isShow"></el-dict>
      </el-form-item>
      <el-form-item label="活动类型" prop="activityType">
        <el-dict code="ACTIVITY_TYPE" :disabled="disabled" v-model="dataForm.activityType"></el-dict>
      </el-form-item>
      <el-form-item label="限制规则" prop="whitelistConfine">
        <el-dict code="WHITELIST_CONFINE" :disabled="disabled" v-model="dataForm.whitelistConfine"></el-dict>
      </el-form-item>
      <el-form-item label="是否付费" prop="needToPay">
        <el-dict code="IS_NOT" :disabled="disabled" v-model="dataForm.needToPay"></el-dict>
      </el-form-item>
      <el-form-item label="白名单类型" prop="whitelistType">
        <el-dict code="WHITELIST_TYPE" :disabled="disabled" v-model="dataForm.whitelistType"></el-dict>
      </el-form-item>
      <el-form-item label="活动日期" prop="activityDate">
        <el-date-picker v-for="(x,y) in dataForm.activityDate.length" :key="x"
          v-model="dataForm.activityDate[y]"
          type="daterange"
          unlink-panels
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd">
        </el-date-picker>
        <div>
          <el-button type="primary" @click="()=>{if(dataForm.activityDate.length > 1) dataForm.activityDate.splice(dataForm.activityDate.length-1)}" icon="el-icon-minus" circle></el-button>
          <el-button type="primary" @click="()=>{if(6 > dataForm.activityDate.length) dataForm.activityDate.push([])}" icon="el-icon-plus" circle></el-button>
        </div>
      </el-form-item>
      <el-form-item label="活动具体时间" prop="activityTime">
        <el-time-picker v-for="(x,y) in dataForm.activityTime.length" :key="x"
          is-range
          v-model="dataForm.activityTime[y]"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          format="HH:mm"
          value-format="HH:mm"
          placeholder="选择时间范围">
        </el-time-picker>
        <div>
          <el-button type="primary" @click="()=>{if(dataForm.activityTime.length > 1) dataForm.activityTime.splice(dataForm.activityTime.length-1)}" icon="el-icon-minus" circle></el-button>
          <el-button type="primary" @click="()=>{if(6 > dataForm.activityTime.length) dataForm.activityTime.push(['00:00', '23:59'])}" icon="el-icon-plus" circle></el-button>
        </div>
      </el-form-item>
      <el-form-item label="微信优惠标识" prop="goodsTag">
        <el-input v-model="dataForm.goodsTag" :disabled="disabled" placeholder="微信优惠标识"></el-input>
      </el-form-item>

      <el-form-item label="活动链接" prop="url">
        <el-input v-model="dataForm.url" :disabled="disabled" placeholder="活动链接"></el-input>
      </el-form-item>

      <el-form-item label="简明介绍" prop="brief">
        <el-input v-model="dataForm.brief" :disabled="disabled" placeholder="简明介绍"></el-input>
      </el-form-item>
      <el-form-item label="活动标签" prop="extend1">
        <el-input v-model="dataForm.extend1" :disabled="disabled" placeholder="简明介绍1"></el-input>
      </el-form-item>
      <el-form-item label="简明介绍2" prop="extend2">
        <el-input v-model="dataForm.extend2" :disabled="disabled" placeholder="简明介绍2"></el-input>
      </el-form-item>
      <el-form-item label="活动配置" prop="config">
        <el-input v-model="dataForm.config" :disabled="disabled" placeholder="活动配置(josn格式)"></el-input>
      </el-form-item>
      <el-form-item label="活动规则" prop="strategy">
        <ueditor v-model="dataForm.strategy" :disabled="disabled" placeholder="商品描述"></ueditor>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        dateCount: 1,
        timeCount: 1,
        dates: {
          dateOne:[],
          dateTwo:[],
          dateThree:[],
          dateFour: [],
          dateFive: []
        },
        times: {
          timeOne:[],
          timeTwo:[],
          timeThree:[],
          timeFour: [],
          timeFive: []
        },
        dataForm: {
          id: 0,
          name: '',
          addTime: '',
          updateTime: '',
          whitelistType: '',
          isDelete: '',
          isShow: '',
          url: '',
          activityDate: [],
          activityTime: [],
          goodsTag: '',
          strategy: '',
          brief: '',
          extend1: '',
          extend2: '',
          activityCode:'',
          activityType:'',
          activityRule:'',
          whitelistConfine:'',
          config:'',
          needToPay:''
        },
        dataRule: {
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'}
          ],
          activityCode: [
            {required: true, message: '活动标识不能为空', trigger: 'blur'}
          ],
          isShow: [
            {required: true, message: '是否展示不能为空', trigger: 'blur'}
          ],
          activityType: [
            {required: true, message: '活动类型不能为空', trigger: 'blur'}
          ],
          whitelistConfine: [
            {required: true, message: '限制规则不能为空', trigger: 'blur'}
          ],
          needToPay: [
            {required: true, message: '是否付费不能为空', trigger: 'blur'}
          ],
          activityDate: [
            {required: true, message: '活动日期不能为空', trigger: 'blur'}
          ],
          activityTime: [
            {required: true, message: '活动时间不能为空', trigger: 'blur'}
          ]
        },
        datePicker: this.picker()
      }
    },
    methods: {
      init (id, disabled) {
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/mall/activity/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.activity
                if(!this.dataForm.activityDate || this.dataForm.activityDate === ''){
                  this.dataForm.activityDate = []
                  this.dataForm.activityTime = []
                }else{
                  this.dataForm.activityDate = JSON.parse(this.dataForm.activityDate)
                  this.dataForm.activityTime = JSON.parse(this.dataForm.activityTime)
                }
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              let data;
              data = JSON.parse(JSON.stringify(this.dataForm))
              data.activityDate = JSON.stringify(data.activityDate)
              data.activityTime = JSON.stringify(data.activityTime)
              this.$http({
                url: `/mall/activity/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      },
      picker () {
        return {
          // 可选时间大于等于当前时间
          disabledDate (time) {
            return time.getTime() < Date.now()
          }
        }
      },
    }
  }
</script>
